package com.lxb.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lxb.commonutils.JwtUtils;
import com.lxb.commonutils.MD5;
import com.lxb.educenter.entity.UcenterMember;
import com.lxb.educenter.entity.dto.LoginDto;
import com.lxb.educenter.entity.dto.RegisterDto;
import com.lxb.educenter.mapper.UcenterMemberMapper;
import com.lxb.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxb.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 登录、注册 服务实现类
 *
 * @author lixuebin
 * @since 2022-12-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public String login(LoginDto loginDto) {

        //获取登录邮箱和密码
        String email = loginDto.getEmail();
        String password = loginDto.getPassword();

        //判断邮箱是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);

        //判断查询对象是否为空
        if (ucenterMember == null) {
            //查询不到该邮箱
            throw new GuliException(20001, "登录失败，该邮箱" + email + "未注册");
        }

        //判断查询密码是否正确
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001, "登录失败，密码错误");
        }

        //判断账号是否禁用
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败，改用户被禁用");
        }


        //登录成功，返回通过jwt信息加密的token字段
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }


    /**
     * 注册
     *
     * @param registerDto
     */
    @Override
    public void register(RegisterDto registerDto) {

        //获取数据
        String nickname = registerDto.getNickname(); //昵称
        String email = registerDto.getEmail(); //邮箱
        String password = registerDto.getPassword(); //密码
        String code = registerDto.getCode(); //验证码

        //校验参数
        if (StringUtils.isEmpty(nickname) &&
                StringUtils.isEmpty(email) &&
                StringUtils.isEmpty(password) &&
                StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "注册失败，请重新输入");
        }

        //1.校验校验验证码
        //从redis获取发送的验证码
        Object emailCode = redisTemplate.opsForValue().get(email);
        if (!code.equals("" + emailCode)) {
            throw new GuliException(20001, "验证错误，请重新输入！");
        }

        //2.是否注册过该email

        //不允许同一个email，注册多次
        //查询数据库中是否注册过该email
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);

        Integer countEmail = baseMapper.selectCount(wrapper);
        if (countEmail.intValue() > 0) {
            throw new GuliException(20001, "该邮箱已注册,请登录");
        }

        //3.新增用户数据
        UcenterMember member = new UcenterMember();
        //通过bean复制
        BeanUtils.copyProperties(registerDto, member);
        //MD5加密密码
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);

        //保存
        baseMapper.insert(member);

    }

    /**
     * 判断openid是否存在
     *
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("openid", openid);

        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    /**
     * 根据openid,更新字段
     *
     * @param member
     */
    @Override
    public void updateByOpenid(UcenterMember member) {

        UpdateWrapper<UcenterMember> wrapper = new UpdateWrapper();
        wrapper.set("nickname", member.getNickname()).set("avatar", member.getAvatar());
        wrapper.eq("openid", member.getOpenid());

        baseMapper.update(null, wrapper);
    }

    /**
     * 统计某天注册人数
     *
     * @param day
     * @return
     */
    @Override
    public Integer CountRegister(String day) {
        String start = day + " 00:00:00";
        String end = day + " 23:59:59";
        Integer countRegister = baseMapper.CountRegister(start,end);
        return countRegister;
    }
}
